<template>
	<view class="warp">
		
		
		<view class="list" >
			<view class="l_text" >昵称</view>
			<view class="r_box" >
				<input class="input" type="text"  v-model="user_nickname" maxlength="20" />
			</view>
		</view>
		<view class="list" @click="chooseImage()">
			<view class="imgtitle">头像</view>
			<image  class="sc_img" style="width:80rpx;height:80rpx;" :src="user_avatar" mode=""></image>
		</view>
	
		<view class="form_btn" @click="save">保存</view>
	</view>
</template>

<script>
	export default {
		
		data() {
			return {
				userinfo:'',
				user_nickname:'',
				user_avatar:'',
				url:this.weburl+'weixin/fileUpload'
			}
		},
		onLoad() {
			
			uni.setNavigationBarTitle({
			    title: '个人资料'
			});
			this.userinfo=uni.getStorageSync('user');
			this.user_avatar=this.userinfo.userAvatar?this.userinfo.userAvatar:'';
			this.user_nickname=this.userinfo.userNickname?this.userinfo.userNickname:'';
		},
		methods:{
			chooseImage(){
				
				var that=this
				uni.chooseImage({
					count: 1,
					success: (res)=> {
						const tempFilePaths = res.tempFilePaths;
						this.user_avatar=res.tempFilePaths[0];
						uni.showLoading({
							title:'上传中',
						})
						uni.uploadFile({
							url: that.url, //仅为示例，非真实的接口地址
							filePath: res.tempFilePaths[0],
							header: {
								'token': uni.getStorageSync('token')
							},
							name: 'file',
							success: (uploadFileRes) => {
								uni.hideLoading();
								that.user_avatar=JSON.parse(uploadFileRes.data).data;
								
							}
						});
					}
				})
			},
			save(){
				var that=this;
				
				if(this.user_nickname==''||this.user_nickname==null||this.user_nickname==undefined){
					uni.showToast({
						icon:'none',
						title:'请输入昵称'
					})
					return;
				}
				
				var that=this;
				this.$http.get('user/saveUserInfo',{userNickname:this.user_nickname,userAvatar:this.user_avatar}).
				then(function (response) {
					if(response.code==2){
						uni.showToast({
							icon:'none',
							title:response.msg
						});
						return;
					}else{
						uni.showToast({
							icon:'none',
							title:'保存成功'
						});
						var clock=setInterval(function(){
							clearInterval(clock);
							uni.navigateBack();
						},1500);
						return;
					}
				}).catch(function (error) {
					console.log(error);
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.warp{
		padding: 0 20rpx;
		.list1{
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid #ebebeb;
			height: 120rpx;
			.t_img{
				width: 90rpx;
				height: 90rpx;
				border-radius: 50%;
			}
			.l_text{
				width: 140rpx;
				font-size: 30rpx;
				color: #454545;
			}
		}
		.list{
			display: flex;
			align-items: center;
			justify-content: space-between;
			height: 100rpx;
			border-bottom: 1px solid #ebebeb;
			.l_text{
				width: 140rpx;
				font-size: 30rpx;
				color: #454545;
			}
			.r_box{
				overflow: hidden;
				height: 100%;
				align-items: center;
				display: flex;
				font-size: 30rpx;
				color: #a5a5a5;
				.r_img{
					width: 16rpx;
					height: 30rpx;
				}
				.input{
					font-size: 30rpx;
					text-align: left;
				}
			}
		}
		.form_btn{
			width: 650rpx;
			height: 90rpx;
			border-radius: 45px;
			background: #102d57;
			font-size: 36rpx;
			color: #fff;
			margin: 150rpx auto 0;
			text-align: center;
			line-height: 90rpx;
		}
	}
</style>
